#ifndef EVENTLOOP_H
#define EVENTLOOP_H

#include <sys/epoll.h>
#include <functional>
#include <map>
#include <memory>
#include <stdio.h>
#include <iostream>
#include <unistd.h>

#include "TcpConnection.hpp"

#define EVENT_ARRAY_LEN 520
class TcpConnection;

using ConnectionPtr = std::shared_ptr<TcpConnection>;
using CallBackPtr = std::shared_ptr<std::function<void(ConnectionPtr)>>;

using NormalCallBack = std::shared_ptr<std::function<void()>>;

class EventLoop {
public:
    EventLoop(){
        epfd_ = epoll_create(1);
    }
    ~EventLoop() {
        close(epfd_);
    }

    int addEvent(int socketfd, unsigned int events, CallBackPtr callback);
    int delEvent(int socketfd);
    int modEvent(int socketfd, unsigned int events, CallBackPtr callback);

    int addNormalEvent(int socketfd, unsigned int events, NormalCallBack callback);
    int delNormalEvent(int socketfd);
    int modNormalEvent(int socketfd, unsigned int events, NormalCallBack callback);

    int getConnectionNum(){
        return connections_.size();
    }

    void run();
    // void cleanConnections();

private:
    int epfd_;
    epoll_event event_array_[EVENT_ARRAY_LEN];
    std::map<int, CallBackPtr> socketCallBack_;
    std::map<int, ConnectionPtr> connections_;
    std::map<int, NormalCallBack> normalCallBack_;

};




#endif